package com.controller;

import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.entity.User;
import com.service.UserService;
import com.util.PageUtil;
import com.util.UploadUtil;

/**
 * 用户相关接口
 */
@Controller
@RequestMapping("/user")
public class UserController{
	
	@Autowired
	private UserService userService;
	
	
	/**
	 * 列表
	 * @param request
	 * @return
	 */
	@GetMapping("/list")
	public String list(HttpServletRequest request,
			@RequestParam(required=false, defaultValue="1") int page,
			@RequestParam(required=false, defaultValue="10") int size) {
		request.setAttribute("list", userService.getList(page, size));
		request.setAttribute("pageHtml", PageUtil.packHtml(request, userService.getTotal(), page, size));
		return "/jsp/user_list.jsp";
	}
	
	/**
	 * 添加
	 * @param request
	 * @return
	 */
	@GetMapping("/add")
	public String add() {
		return "/jsp/user_add.jsp";
	}
	
	/**
	 * 保存
	 * @param user
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@PostMapping("/save")
	public String save(User user, MultipartFile file, HttpServletRequest request) throws Exception {
		if (Objects.nonNull(userService.getByUsername(user.getUsername()))) {
			request.setAttribute("msg", "账号已存在");
			return add();
		}
		if (Objects.nonNull(file) && !file.isEmpty()) {
			user.setIcon(UploadUtil.upload(file)); // 上传图片
		}
		userService.add(user);
		return "redirect:/user/list";
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param request
	 * @return
	 */
	@GetMapping("/edit")
	public String edit(int id, HttpServletRequest request) {
		request.setAttribute("user", userService.get(id));
		return "/jsp/user_edit.jsp";
	}
	
	/**
	 * 修改
	 * @param id
	 * @param user
	 * @param request
	 * @return
	 */
	@PostMapping("/update")
	public String update(User user, MultipartFile file) throws Exception {
		if (Objects.nonNull(file) && !file.isEmpty()) {
			user.setIcon(UploadUtil.upload(file)); // 上传图片
		}
		userService.update(user);
		return "redirect:/user/list";
	}
	
	/**
	 * 重置密码
	 * @param id
	 * @param user
	 * @param request
	 * @return
	 */
	@GetMapping("/reset")
	public String reset(int id, HttpServletRequest request) {
		request.setAttribute("user", userService.get(id));
		return "/jsp/user_reset.jsp";
	}
	
	/**
	 * 重置密码
	 * @param id
	 * @param user
	 * @param request
	 * @return
	 */
	@PostMapping("/reset")
	public String reset(int id, String password) {
		userService.reset(id, password);
		return "redirect:/user/list";
	}
	
	/**
	 * 删除
	 * @param request
	 * @return
	 */
	@RequestMapping("/delete")
	public String delete(int id) {
		userService.delete(id);
		return "redirect:/user/list";
	}

	
}